<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>getMonthsDiffBetweenDates</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const getMonthsDiffBetweenDates = (dateInitial, dateFinal) =>
            Math.max(
                (dateFinal.getFullYear() - dateInitial.getFullYear()) * 12 +
                dateFinal.getMonth() -
                dateInitial.getMonth(),
                0
            );
        console.log(getMonthsDiffBetweenDates(new Date('2017-12-13'), new Date('2018-04-29')));
    </script>
</body>

</html>